<?
$countries = Country::find()->getAll();
Q::register($countries, 'countries');


function renderCountryId($id)
{
    $countries = Q::registry('countries');

    $ret = '<select id="country_id" name="country_id"><option value="">--请选择--</option>';
    foreach ($countries as $c) {
        $ret .= '<option value="' . $c->id() . '" utc_offset="' . $c->utc_offset . '"';
        if ($c->id() == $id) {
            $ret .= ' selected="selected"';
        }
        $ret .= '>' . $c->name . ' ' . $c->chinese_name . '</option>';
    }

    $ret .= '</select>';
    return $ret;
}

function renderBindPlanStatusChecked($val){
    if($val === '0'){
        return ' checked=checked';
    }
    return '';
}

?>

<?php $this->_extends('_layouts/default_layout'); ?>

<?php $this->_block('title'); ?>客户管理<?php $this->_endblock(); ?>

<?php $this->_block('js'); ?>
<script type="text/javascript">
    $(function() {
        $('#clearFilters').click(function() {
            $('#filterForm').find('select, input:text').val('');
            $('#bindPlanStatus').attr('checked', false);
        });

        $('#filterForm').submit(function() {
            var location = this.action+'/index';
            var para = $('#filterForm').serializeArray();
            $.each(para,function(i, field){
                if(field.value){
                    location += '/'+field.name+'/'+field.value
                }
            });
            location = location.replace('+','%20');
            window.location = location;
            return false;
        });
    });


</script>
<?php $this->_endblock('js'); ?>

<?php $this->_block('contents'); ?>

<form id="filterForm" action="<?=url('.')?>">
    <table>
        <tr>
            <td>国家</td>
            <td><?= renderCountryId($_ctx->country_id) ?></td>
            <td>行业</td>
            <td><?=Q::control('dropdownlist', 'profession_id', array('items' => Profession::items(), 'value' => $_ctx->profession_id, 'emptyText' => '--请选择--'))?></td>
        </tr>
    </table>
    <table>
        <tr>
            <td>客户等级</td>
            <td><input name="grade" value="<?=$_ctx->grade?>" size="1"></td>
            <td>客户编号</td>
            <td><input name="customer_num" value="<?=$_ctx->customer_num?>" size="5"></td>
            <td>客户名</td>
            <td><input name="full_name" value="<?=$_ctx->full_name?>"></td>
            <td>email <input name="email" value="<?=$_ctx->email?>"></td>
            <td><input id="bindPlanStatus" type="checkbox" value="0" name="bindPlanStatus"<?=renderBindPlanStatusChecked($_ctx->bindPlanStatus)?>><label for="bindPlanStatus" style="display: inline;">未绑定邮件计划的客户</label></td>
        </tr>
    </table>
    <input type="button" value="清空filters" id="clearFilters">
    <input type="submit" value="搜索客户"><br><br>
</form>

<table class=dataTable id=customers>
    <thead>
    <tr>
        <th>客户编号</th>
        <th>全名</th>
        <th>国家</th>
        <th>行业</th>
        <th>等级</th>
        <th>网站</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    <? foreach ($customers as $c): ?>
    <tr>
        <td><?=$c['customer_num']?></td>
        <td><?=$c['full_name']?></td>
        <td><?=$c['country']->chinese_name?></td>
        <td><?=$c['profession']->name?></td>
        <td><?=$c['grade']?></td>
        <td><a href="<?=$c->web_site()?>" target="_blankl"><?=$c['web_site']?></a></td>
        <td align=center>
            <a href="<?=url('customer/edit', array('cid' => $c->id()))?>" target="_blank"><img title="编辑"
                                                                                               src="<?=$_BASE_DIR?>img/icon/Edit.png"/></a>
            <a href="<?=url('customer/remove', array('cid' => $c->id()))?>"><img title="删除"
                                                                                 src="<?=$_BASE_DIR?>img/icon/Delete.png"/></a>
        </td>
    </tr>
        <? endforeach; ?>
    </tbody>
</table>
<?=
Q::control('pagination', 'my-pagination', array('pagination' => $pagination, 'url_args' => $_GET, 'show_count' => 1))
; ?>

<div id=result>更改成功</div>
<?php $this->_endblock(); ?>